//
//  LSZMyCheckRecordViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/6/8.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZMyCheckRecordViewController.h"

@interface LSZMyCheckRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewCheckRecord;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * arrayCheckRecord;

@property (nonatomic, weak) UIImageView *lineView;

@end

@implementation LSZMyCheckRecordViewController
//找到导航栏最下面黑线视图
- (UIImageView *)getLineViewInNavigationBar:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

-(void)viewWillDisappear:(BOOL)animated
{
    _lineView.hidden = NO;
}
-(void)viewWillAppear:(BOOL)animated
{
    self.page = 1;
    [self getMyCheckRecordData:self.page];

    _lineView.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
}
-(void)getMyCheckRecordData :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic userCheckRecord:@"1" page:[NSString stringWithFormat:@"%zd",(long)page] type:@"1" :^(BOOL isOk, NSString * string, LSZSignRecordManager * manager) {
        if (isOk == YES) {
            if (weakSelf.page == 1) {
                [weakSelf.arrayCheckRecord removeAllObjects];
                if(manager.arrayData.count == 0){
                    weakSelf.tableViewCheckRecord.mj_footer.hidden = YES;
                }else{
                    [weakSelf.arrayCheckRecord setArray:manager.arrayData];
                }
            }
            else{
                [weakSelf.arrayCheckRecord addObjectsFromArray:manager.arrayData];
            }
            if(manager.arrayData.count == 0){
                [weakSelf.tableViewCheckRecord.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.tableViewCheckRecord reloadData];
        }else{
            [weakSelf setStringWarning:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的签到记录";
    self.arrayCheckRecord = [[NSMutableArray alloc]init];
    //获取导航栏下面黑线
    _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];
    __weak typeof(self) weakSelf = self;
    [weakSelf.tableViewCheckRecord registerClass:[LSZSignRecordHeaderCell class] forHeaderFooterViewReuseIdentifier:@"LSZSignRecordHeaderCell"];
    [weakSelf.tableViewCheckRecord registerClass:[LSZNewRecordTableViewCell class] forCellReuseIdentifier:@"LSZNewRecordTableViewCell"];

    weakSelf.tableViewCheckRecord.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 1;
            [weakSelf getMyCheckRecordData:self.page];
            [weakSelf.tableViewCheckRecord.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    weakSelf.tableViewCheckRecord.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page++;
            [weakSelf getMyCheckRecordData:self.page];
            [weakSelf.tableViewCheckRecord.mj_footer endRefreshing];
        });
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (ScreenWidth == 320) {
        //5
        return 110+10;
    }else if(ScreenWidth == 375){
        // 6 7
        return 120+10;
    }else {
        //plus
        return 128+10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if (ScreenWidth == 320) {
            //5
            return 40;
        }else if(ScreenWidth == 375){
            // 6 7
            return 45;
        }else {
            //plus
            return 50;
        }
    }else{
        if (ScreenWidth == 320) {
            //5
            return 55;
        }else if(ScreenWidth == 375){
            // 6 7
            return 59;
        }else {
            //plus
            return 63;
        }
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayCheckRecord.count+1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LSZSignRecordHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZSignRecordHeaderCell"];
    [cell.imageViewHeader sd_setImageWithURL:[NSURL URLWithString:self.headerUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.labelName.text = self.userName;
    cell.labelRanking.text = [NSString stringWithFormat:@"当前排名:%@  |  累计签到:%@次",self.ranking , self.cumulativeNumber];
    return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellCheck"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellCheck"];
        }
        cell.textLabel.text = @"我的签到记录";
        cell.textLabel.textColor = titleColorLSZ;
        if (ScreenWidth == 320) {
            //5
            cell.textLabel.font = FONT_(14.5);
        }else if(ScreenWidth == 375){
            // 6 7
            cell.textLabel.font = FONT_(15);
        }else {
            //plus
            cell.textLabel.font = FONT_(15.5);
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZSignRecordMg * mg = nil;
        mg = self.arrayCheckRecord[indexPath.row-1];
        LSZNewRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNewRecordTableViewCell" forIndexPath:indexPath];
        cell.labelNumber.text =  [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        if ([mg.credit isEqualToString:@""]||[mg.credit isEqualToString:@"0"]) {
            cell.labelTitle.text =  @"未统计";
        }else{
            cell.labelTitle.text = [NSString stringWithFormat:@"签到获得%@福音值",mg.credit];
        }
        cell.labelTime.text = mg.uptime;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(UITableView *)tableViewCheckRecord
{
    if (_tableViewCheckRecord == nil) {
        _tableViewCheckRecord = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-HOME_INDICATOR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewCheckRecord.delegate = self;
        _tableViewCheckRecord.dataSource = self;
        _tableViewCheckRecord.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewCheckRecord.estimatedRowHeight =0;
        _tableViewCheckRecord.estimatedSectionFooterHeight =0;
        _tableViewCheckRecord.estimatedSectionHeaderHeight =0;
        _tableViewCheckRecord.separatorColor = divider;
        [self.view addSubview:_tableViewCheckRecord];
    }
    return _tableViewCheckRecord;
}


@end
